package com.netistate.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.netistate.dao.ClientContactDao;
import com.netistate.dao.DemoDao;
import com.netistate.dao.DocDao;
import com.netistate.dao.LaborDao;
import com.netistate.dao.ProductDao;
import com.netistate.dao.ProjectBlogDao;
import com.netistate.dao.ProjectDao;
import com.netistate.dao.ProjectServiceDetailDao;
import com.netistate.dao.ProjectServicesDao;
import com.netistate.dao.ProjectTypeDefineDao;
import com.netistate.dao.SaleDao;
import com.netistate.dao.TechNoteDao;
import com.netistate.dao.UserDao;
import com.netistate.dao.UserProjectDao;
import com.netistate.pojo.ClientContact;
import com.netistate.pojo.Demo;
import com.netistate.pojo.Doc;
import com.netistate.pojo.Labor;
import com.netistate.pojo.Product;
import com.netistate.pojo.Project;
import com.netistate.pojo.ProjectBlog;
import com.netistate.pojo.ProjectServiceDetail;
import com.netistate.pojo.ProjectServices;
import com.netistate.pojo.ProjectTypeDefine;
import com.netistate.pojo.Sale;
import com.netistate.pojo.TechNote;
import com.netistate.pojo.User;
import com.netistate.pojo.UserProject;

@Service
@Transactional
public class ProjectService {

	private ProjectDao projectDao;
	private UserProjectDao userProjectDao;
	private SaleDao saleDao;
	private TechNoteDao techNoteDao;
	private ProjectBlogDao projectBlogDao;
	private ClientContactDao clientContactDao;
	private DocDao docDao;
	private ProjectTypeDefineDao projectTypeDefineDao;
	private ProjectServicesDao projectServicesDao;
	private ProjectServiceDetailDao projectServiceDetailDao;
	private UserDao userDao;
	private ProductDao productDao;
	private DemoDao demoDao;
	private LaborDao laborDao;
	
	private final String MEMBER = "member";
	private final String MANAGER = "manager";
	
	/**
	 * 登录后默认查找一个月内的项目信息
	 * @return
	 */
	public List<Project> findProjectListInOneMonth() {
		return projectDao.findProjectListInOneMonth();
	}
	
	/**
	 * 过滤查询projectList,条件为阶段为售前
	 * @param string
	 * @return
	 */
	public List<Project> findProjectByProjectProcess(String sq) {
		return projectDao.findProjectByProjectProcess(sq);
	}
	
	/**
	 * 查找所有的项目
	 * @return
	 */
	public List<Project> findAll() {
		return projectDao.findAllProject();
	}

	/**
	 * 添加项目
	 * @param p
	 */
	public void saveProject(Project p) {
		projectDao.save(p);
	}
	
	/**
	 * 根据条件来查找项目
	 * @param projectManager
	 * @param starttime
	 * @param endtime
	 * @param projectProcess
	 * @return projectList
	 */
	public List<Project> queryProjectByCondition(String projectManager,
			String starttime, String endtime, String projectProcess) {
		return projectDao.queryProjectByCondition(projectManager,starttime,endtime,projectProcess);
	}
	/**
	 * 把项目和项目管理者存到关系表里
	 * @param u
	 * @param p
	 */
	public void saveUserAndProject(UserProject up) {
		up.setRole(MANAGER);
		userProjectDao.save(up);
	}
	/**
	 * 保存项目成员
	 * @param up
	 */
	public void saveUserAndProjectMember(UserProject ups) {
		ups.setRole(MEMBER);
		userProjectDao.save(ups);
	}
	
	/**
	 * 根据id去找项目
	 * @param pid
	 * @return project
	 */
	public Project findProjectById(int pid) {
		return projectDao.findById(pid);
	}
	
	/**
	 * 根据pid去UserProject找对应项目所对应的人
	 * @param pid
	 * @return UserProject
	 */
	public List<UserProject> findUserProjectByPid(int pid) {
		return userProjectDao.findUserProjectByPid(pid);
	}
	
	/**
	 * 根据项目id去删除项目,首先级联删除关系表中内容
	 * @param id
	 */
	public void del(int pid) {
		projectDao.del(pid);
	}
	/**
	 * 首先级联删除关系表
	 * @param up
	 */
	public void deleteUserProject(UserProject up) {
		userProjectDao.del(up);
	}
	
	/**
	 * 查找项目下的所有销售表
	 * @param pid
	 * @return
	 */
	public List<Sale> findAllSaleByPid(int pid) {
		return saleDao.findAllSaleByPid(pid);
	}
	
	/**
	 * 保存项目下的销售表
	 * @param s
	 */
	public void saveSale(Sale s) {
		saleDao.save(s);
	}
	
	/**
	 * 保存项目下的技术备忘录
	 * @param t
	 */
	public void saveTechNote(TechNote t) {
		techNoteDao.save(t);
	}
	/**
	 * 保存项目相关的日志
	 * @param pb
	 */
	public void saveProjectBlog(ProjectBlog pb) {
		projectBlogDao.save(pb);
	}
	
	/**
	 * 保存项目下的联系人
	 * @param cc
	 */
	public void saveClientContact(ClientContact cc) {
		clientContactDao.save(cc);
	}
	
	//--------------------------------------技术备忘录----------------------------------
	
	
	/**
	 * 根据客户名称模糊查询技术备忘录
	 * @param blurByClientName
	 * @return
	 */
	public List<TechNote> findTechNoteByClientName(String clientName) {
		return techNoteDao.findTechNoteByClientName(clientName);
	}
	
	/**
	 * 根据项目名称模糊查询技术备忘录
	 * @param blurByProjectName
	 * @return
	 */
	public List<TechNote> findTechNoteByProjectName(String projectName) {
		return techNoteDao.findTechNoteByProjectName(projectName);
	}
	
	/**
	 * 删除技术备忘录
	 * @param parseInt
	 */
	public void delTechNote(int tid) {
		techNoteDao.del(tid);
	}
	
	
	/**
	 * 根据条件查询技术备忘录，不在项目下
	 * @param pid
	 * @param projectProcess
	 * @param begintime
	 * @param endtime
	 * @param endtime2 
	 * @param begintime2 
	 * @param doer 
	 * @return 
	 */
	public List<TechNote> findTechNoteByCondition(String pid, String state, String level,
			String reporter, String doer, String begintime, String endtime) {
		return techNoteDao.findTechNoteByCondition(pid,state,level,reporter,doer,begintime,endtime);
	}
	/**
	 * 查询所有的技术备忘录
	 * @return
	 */
	public List<TechNote> findAllTechNote() {
		return techNoteDao.findAllTechNoteByOrders();
	}
	/**
	 * 根据tid去找技术备忘录
	 * @param tid
	 * @return
	 */
	public TechNote findTechNoteById(int tid) {
		return techNoteDao.findById(tid);
	}
	
	/**
	 * 更新修改内容的技术备忘录
	 * @param t
	 */
	public void updateTechNote(TechNote t) {
		techNoteDao.save(t);
	}
	/**
	 * 根据项目id去查找技术备忘录
	 * @param pid
	 * @return
	 */
	public List<TechNote> finTechNoteByProjectId(int pid) {
		return techNoteDao.findTechNoteByProjectId(pid);
	}
	
	/**
	 * 在项目下去有条件的查询技术备忘录
	 * @param state
	 * @param level
	 * @param reporter
	 * @param doer
	 * @param begintime
	 * @param endtime
	 * @return
	 */
	public List<TechNote> findProjectTechNoteByCondition(String id,String state,
			String level, String reporter, String doer, String begintime,
			String endtime) {
		return techNoteDao.findProjectTechNoteByCondition(id,state,level,reporter,doer,begintime,endtime);
	}
	
	//-------------文档---------
	
	/**
	 * 根据psid去删除文档里的记录
	 * @param pssid
	 */
	public void delProjectServicesDoc(int pssid) {
		docDao.delDocByPSId(pssid);
	}
	
	/**
	 * 切换项目下的文档
	 * @param parseInt
	 * @return
	 */
	public List<Doc> findAllDocByPid(int pid) {
		return docDao.findAllDocByPid(pid);
	}
	
	/**
	 * 查询所有文档
	 * @return
	 */
	public List<Doc> findAllDoc() {
		return docDao.findAll();
	}
	/**
	 * 保存文档数据
	 * @param d
	 */
	public void saveDoc(Doc d) {
		docDao.save(d);
	}
	
	/**
	 * 根据条件去查询文档
	 * @param projectName
	 * @param begintime
	 * @param endtime
	 * @return
	 */
	public List<Doc> findDocByCondition(String projectName, String begintime,
			String endtime) {
		return docDao.findDocByCondition(projectName,begintime,endtime);
	}
	
	/**
	 * 根据id去找文档
	 * @param parseInt
	 * @return
	 */
	public Doc findDocById(int docid) {
		return docDao.findById(docid);
	}
	
	
	//-------------项目接口人-------------
	
	/**
	 * 删除接口人
	 * @param parseInt
	 */
	public void delClient(int cid) {
		clientContactDao.del(cid);
	}
	
	/**
	 * 根据Pid去找项目下的客户接口人
	 * @param pid
	 * @return
	 */
	public List<ClientContact> findClientContactByProjectId(int pid) {
		return clientContactDao.findClientContactByProjectId(pid);
	}
	/**
	 * 根据cid去查找客户接口人
	 * @param parseInt
	 * @return
	 */
	public ClientContact findClientContactByCid(int cid) {
		return clientContactDao.findById(cid);
	}
	
	//---------------服务记录定义表----------------
	/**
	 * 根据serNo去找对应的infoNo并排序
	 * @param serNo
	 * @return
	 */
	public List<ProjectTypeDefine> findProjectTypeDefineBySerNo(String serNo) {
		return projectTypeDefineDao.findProjectTypeDefineBySerNo(serNo);
	}
	//------------------ProjecetServices------------------------
	
	/**
	 * 向ProjectServices添加值
	 * @param ps
	 */
	public void saveProjectServices(ProjectServices ps) {
		projectServicesDao.save(ps);
	}
	/**
	 * 查找所有的ProjectServices
	 * @param pid
	 * @return
	 */
	public List<ProjectServices> findAllProjectServicesByPid(int pid) {
		return projectServicesDao.findAllProjectServicesByPid(pid);
	}
	
	/**
	 * 删除projectServices
	 * @param pssid
	 */
	public void delProjectServices(int pssid) {
		projectServicesDao.del(pssid);
	}
	
	//----------------------ProjectServiceDetail-----------------------------
	
	/**
	 * 根据psid去删除服务记录表定义的内容
	 * @param pssid
	 */
	public void delProjectServicesDetail(int pssid) {
		projectServiceDetailDao.delPSDByPSId(pssid);
	}
	
	/**
	 * 添加ProjectServiceDetail
	 * @param psd
	 */
	public void saveProjectServiceDetail(ProjectServiceDetail psd) {
		projectServiceDetailDao.save(psd);
	}
	/**
	 * 根据ProjectServicesID去找ProjectServices
	 * @param parseInt
	 * @return
	 */
	public ProjectServices findProjectServicesByPSId(int psId) {
		return projectServicesDao.findById(psId);
	}
	
	/**
	 * 根据projectId去找全部的ProjectSerivceDetail
	 * @param projectId
	 * @return
	 */
	public List<ProjectServiceDetail> findProjectServiceDetailByProjectId(
			int projectId) {
		return projectServiceDetailDao.findProjectServiceDetailByProjectId(projectId);
	}
	
	/**
	 * 根据projectId去找全部的ProjectSerivces
	 * @param projectId
	 * @return
	 */
	public List<ProjectServices> findProjectServicesByProjectId(int projectId) {
		return projectServicesDao.findAllProjectServicesByPid(projectId);
	}
	
	/**
	 * 根据服务类型查信息记录
	 * @param projectServiceType
	 * @param projectServiceType2 
	 * @return 
	 */
	public List<ProjectServiceDetail> findProjectServiceByServiceType(String pid, String psType) {
		return projectServiceDetailDao.findProjectServiceByServiceType(pid,psType);
	}
	
	/**
	 * 根据信息类型查
	 * @param pid
	 * @param infoType
	 * @return
	 */
	public List<ProjectServiceDetail> findProjectServiceDetailByInfoType(String pid,
			String infoType) {
		return projectServiceDetailDao.findProjectServiceDetailByInfoType(pid,infoType);
	}
	
	//----------------------------blog
	/**
	 * 查询项目下的日志
	 * @param parseInt
	 * @return
	 */
	public List<ProjectBlog> findAllProjectBlogByPid(int pid) {
		return projectBlogDao.findAllProjectBlogByPid(pid);
	}
	
	/**
	 * 根据projectServiceId去找对应的projectServiceDetail列
	 * @param parseInt
	 * @return
	 */
	public List<ProjectServiceDetail> findProjectServiceDetailByPSId(
			int psid) {
		return projectServiceDetailDao.findProjectServiceDetailByPSId(psid);
	}
	
	/**
	 * 根据username去找user
	 * @param reporter
	 * @return
	 */
	public User findUserByUsername(String reporter) {
		return userDao.findUserByUsername(reporter);
	}
	
	/**
	 * 删除项目级联删除产品
	 * @param pid
	 */
	public void delProductByProjectId(int pid) {
		List<Product> pList = productDao.findProductByProjectId(pid);
		for(Product p : pList){
			productDao.del(p);
		}
		System.out.println("删除产品成功！");
	}
	
	/**
	 * 根据项目id级联删除技术备忘录
	 * @param pid
	 */
	public void delTechNoteByProjectId(int pid) {
		List<TechNote> tList = techNoteDao.findTechNoteByProjectId(pid);
		for(TechNote t : tList){
			techNoteDao.del(t);
		}
		System.out.println("删除技术备忘录成功！");
	}
	
	/**
	 * 根据项目id级联删除客户联系人
	 * @param pid
	 */
	public void delClientContactByProjectId(int pid) {
		List<ClientContact> ccList = clientContactDao.findClientContactByProjectId(pid);
		for(ClientContact cc : ccList){
			clientContactDao.del(cc);
		}
		System.out.println("删除客户联系人成功！");
	}
	
	/**
	 * 根据项目id去级联删除Demo
	 * @param pid
	 */
	public void delDemoByProjectId(int pid) {
		List<Demo> dList = demoDao.findDemoByPid(pid);
		for(Demo d : dList){
			demoDao.del(d);
		}
		System.out.println("删除Demo成功！");
	}
	
	/**
	 * 根据项目id级联删除PSD和PSs
	 * @param pid
	 */
	public void delProjectServicesAndPSDByProjectId(int pid) {

		List<ProjectServices> psList = projectServicesDao.findAllProjectServicesByPid(pid);
		for(ProjectServices ps : psList){
			//先删除projectServicesDetail
			projectServiceDetailDao.delPSDByPSId(ps.getId());
			
			//z再删除Doc
			docDao.delDocByPSId(ps.getId());
			
			//再删除projectServices
			projectServicesDao.del(ps);
		}
		System.out.println("删除ProjectServices成功！");
	}

	/**
	 * 根据项目id去删除ProjectBlog
	 * @param pid
	 */
	public void delprojectBlogByProjectId(int pid) {
		List<ProjectBlog> pbList = projectBlogDao.findAllProjectBlogByPid(pid);
		for(ProjectBlog pb : pbList){
			projectBlogDao.del(pb);
			System.out.println("删除ps完毕");
		}
		System.out.println("删除projectBlog成功！");
	}

	/**
	 * 根据项目Id去级联删除工时
	 * @param pid
	 */
	public void delLaborByProjectId(int pid) {
		System.out.println("开始查询工时");
		List<Labor> lList = laborDao.findLaborByProjectId(pid);
		System.out.println("开始删除工时");
		for(Labor l : lList){
			laborDao.del(l);
		}
		System.out.println("删除工时成功！");
	}
	
	//----
	@Autowired
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	@Autowired
	public void setUserProjectDao(UserProjectDao userProjectDao) {
		this.userProjectDao = userProjectDao;
	}
	@Autowired
	public void setSaleDao(SaleDao saleDao) {
		this.saleDao = saleDao;
	}
	@Autowired
	public void setTechNoteDao(TechNoteDao techNoteDao) {
		this.techNoteDao = techNoteDao;
	}
	@Autowired
	public void setProjectBlogDao(ProjectBlogDao projectBlogDao) {
		this.projectBlogDao = projectBlogDao;
	}
	@Autowired
	public void setClientContactDao(ClientContactDao clientContactDao) {
		this.clientContactDao = clientContactDao;
	}
	@Autowired
	public void setDocDao(DocDao docDao) {
		this.docDao = docDao;
	}
	@Autowired
	public void setProjectTypeDefineDao(ProjectTypeDefineDao projectTypeDefineDao) {
		this.projectTypeDefineDao = projectTypeDefineDao;
	}
	@Autowired
	public void setProjectServicesDao(ProjectServicesDao projectServicesDao) {
		this.projectServicesDao = projectServicesDao;
	}
	@Autowired
	public void setProjectServiceDetailDao(
			ProjectServiceDetailDao projectServiceDetailDao) {
		this.projectServiceDetailDao = projectServiceDetailDao;
	}
	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Autowired
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	@Autowired
	public void setDemoDao(DemoDao demoDao) {
		this.demoDao = demoDao;
	}
	@Autowired
	public void setLaborDao(LaborDao laborDao) {
		this.laborDao = laborDao;
	}

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	

	
	

	

	

	

	

	

	
	

	

	
	
	

	
	

}
